using System;
using System.IO;
using Microsoft.Win32;

namespace Catastrophe.Common.WindowsApi
{
	public class RegistryKeyUtil
	{
		public static string SetAutoRun(string progFullName, string name, bool isAllUser)
		{
			if (!File.Exists(progFullName))
			{
				return "file not exist";
			}
			RegistryKey registryKey = (isAllUser ? Registry.LocalMachine : Registry.CurrentUser);
			RegistryKey registryKey2 = registryKey.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
			if (registryKey2.GetValue(name) == null)
			{
				try
				{
					registryKey2.SetValue(name, progFullName);
					registryKey.Close();
					return "";
				}
				catch (Exception innerException)
				{
					throw new Exception("failed", innerException);
				}
			}
			return "registrykey already exist";
		}

		public static string CancelAutoRun(string name, bool isAllUser)
		{
			RegistryKey registryKey = (isAllUser ? Registry.LocalMachine : Registry.CurrentUser);
			RegistryKey registryKey2 = registryKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", writable: true);
			try
			{
				if (registryKey2.GetValue(name) == null)
				{
					return "registrykey not exist";
				}
				registryKey2.DeleteValue(name);
				registryKey.Close();
				return "";
			}
			catch (Exception innerException)
			{
				throw new Exception("failed", innerException);
			}
		}
	}
}
